#include "groupsessiondetailwidget.h"

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget *parent)
    :QDialog(parent)
{
    // 设置基本属性
    this->setFixedSize(410, 600);
    this->setWindowTitle("群组详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 创建布局管理器
    QVBoxLayout *vlayout = new QVBoxLayout();
    vlayout->setSpacing(10);
    vlayout->setContentsMargins(50, 5, 50, 50);
    vlayout->setAlignment(Qt::AlignTop);
    this->setLayout(vlayout);

    // 创建滚动区域
    _scrollArea = new QScrollArea();
    _scrollArea->setWidgetResizable(true);
    _scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px; }");
    _scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(255, 255, 255); }");
    _scrollArea->setFixedSize(310, 350);
    _scrollArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _scrollArea->setStyleSheet("QWidget { background-color: transparent; border: none; }");

    // 创建滚动区域额的widget
    _container = new QWidget();
    _scrollArea->setWidget(_container);

    // 创建container的网格布局
    _glayout = new QGridLayout();
    _glayout->setSpacing(10);
    _glayout->setContentsMargins(0, 0, 0, 0);
    _glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    _container->setLayout(_glayout);

    vlayout->addWidget(_scrollArea);

    // 创建添加按钮
    _addBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加", true);
    _glayout->addWidget(_addBtn, 0, 0);

    // 添加群聊名称
    _groupNameTag = new QLabel();
    _groupNameTag->setText("群聊名称");
    _groupNameTag->setFixedHeight(50);
    _groupNameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    _groupNameTag->setStyleSheet("QLabel { font-weight: 700; font-size: 16px; }");
    _groupNameTag->setAlignment(Qt::AlignBottom);
    vlayout->addWidget(_groupNameTag);

    // 添加真实群聊名称
    QHBoxLayout *hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->addLayout(hlayout);

    _groupNameLabel = new QLabel();
    _groupNameLabel->setFixedHeight(50);
    _groupNameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    _groupNameLabel->setStyleSheet("QLabel { font-size: 18px; }");
    hlayout->addWidget(_groupNameLabel, 0, Qt::AlignLeft | Qt::AlignVCenter);

    _modifyBtn = new QPushButton();
    _modifyBtn->setFixedSize(20, 20);
    _modifyBtn->setIconSize(QSize(20, 20));
    _modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    _modifyBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed { background-color: rgb(230, 230, 230); }");
    hlayout->addWidget(_modifyBtn, 0, Qt::AlignRight | Qt::AlignVCenter);

    // 添加退出群聊按钮
    _exitGroupBtn = new QPushButton();
    _exitGroupBtn->setText("退出群聊");
    _exitGroupBtn->setFixedHeight(50);
    _exitGroupBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString btnStyle = "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; background-color: transparent; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }";
    _exitGroupBtn->setStyleSheet(btnStyle);
    vlayout->addWidget(_exitGroupBtn);

#if TEST_UI
    _groupNameLabel->setText("人类吃喝行为研究小组");

    for(int i = 0; i < 30; i++)
    {
        UserInfo userInfo;
        userInfo._user_id = QString::number(1000 + i);
        userInfo._mail = "222222222";
        userInfo._nickname = "张三" + QString::number(i);
        userInfo._avatar = QIcon(":/resource/image/defaultAvatar.png");
        AvatarItem *item = new AvatarItem(userInfo);
        addMember(item);
    }
#endif
}

void GroupSessionDetailWidget::addMember(AvatarItem *avatarItem)
{
    int row = _memberNumber / 4;
    int col = _memberNumber % 4;
    _glayout->addWidget(avatarItem, row, col);
    _memberNumber++;
}
